import 'dart:convert';

import 'package:fate_blind_box/chat_view/chat_net_image.dart';
import 'package:fate_blind_box/config/app_style.dart';
import 'package:fate_blind_box/theme/theme_color.dart';
import 'package:fate_blind_box/ui/Home/activity/act_details_page.dart';
import 'package:fate_blind_box/ui/Home/activity/act_list_model.dart';
import 'package:fate_blind_box/utils/provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:im_flutter_sdk/im_flutter_sdk.dart';

import 'ahu_text.dart';

class CustomMessageView extends StatelessWidget {
  final EMMessage message;
  final EMCustomMessageBody customMessageBody;
  final EMUserInfo? userInfo;

  const CustomMessageView({
    super.key,
    required this.message,
    required this.customMessageBody,
    this.userInfo,
  });

  @override
  Widget build(BuildContext context) {
    return customView();
  }

  /// 返回自定义消息
  Widget customView() {
    var content = customMessageBody.params?['content'] ?? '';
    bool isNotSelf = message.direction == MessageDirection.RECEIVE;
    if (content.isNotEmpty) {
      Map<String, dynamic> jsonContent = jsonDecode(content);
      FuturePopular dataModel = FuturePopular.fromJson(jsonContent);
      String actiCover = dataModel.actiCover ?? '';
      String title = dataModel.actiTitle ?? '';
      String startTime = dataModel.actiTime ?? '';
      String address = dataModel.actiAddr ?? '';
      String actiCost = dataModel.actiCost ?? '';
      return Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          // message.direction == MessageDirection.RECEIVE
          //     ? personWidget()
          //     : const SizedBox(),
          SizedBox(
              height: message.direction == MessageDirection.RECEIVE ? 6.w : 0),
          Container(
            padding:
                const EdgeInsets.only(left: 16, right: 13, top: 13, bottom: 12),
            constraints: BoxConstraints(maxWidth: 1.sw - 69 - 62 - 20),
            alignment: isNotSelf ? Alignment.centerLeft : Alignment.centerRight,
            decoration: BoxDecoration(
              color: Provider.userEaseName.isNotEmpty &&
                      message.from != Provider.userEaseName
                  ? const Color(0xffffffff)
                  : const Color(0xff98e165),
              borderRadius: BorderRadius.only(
                topLeft: Radius.circular(isNotSelf ? 0 : 16.w),
                topRight: Radius.circular(isNotSelf ? 16.w : 0),
                bottomLeft: Radius.circular(16.w),
                bottomRight: Radius.circular(16.w),
              ),
            ),
            child: Provider.userEaseName.isNotEmpty &&
                    message.from != Provider.userEaseName
                ? getCoverImgView(
                    actiCover: actiCover,
                    title: title,
                    address: address,
                    startTime: startTime,
                    actiCost: actiCost,
                    crossAxis: CrossAxisAlignment.end,
                    mainAxis: MainAxisAlignment.end,
                    dataModel: dataModel,
                  )
                : getCoverImgView(
                    actiCover: actiCover,
                    title: title,
                    address: address,
                    startTime: startTime,
                    actiCost: actiCost,
                    crossAxis: CrossAxisAlignment.start,
                    mainAxis: MainAxisAlignment.start,
                    dataModel: dataModel,
                  ),
          ),
        ],
      );
    }
    return Container();
  }

  /// 成员信息
  Widget personWidget() {
    if (userInfo?.gender == 2) {
      return Row(
        mainAxisAlignment: MainAxisAlignment.start,
        children: [
          Container(
            padding: EdgeInsets.symmetric(vertical: 2.w),
            margin: EdgeInsets.only(right: 4.w),
            decoration: BoxDecoration(
              color: '#FFF3FD'.stringToColor,
              borderRadius: BorderRadius.circular(8.w),
            ),
            child: Row(
              mainAxisSize: MainAxisSize.min,
              children: [
                SizedBox(width: 6.w),
                Image.asset(
                  'images/user/txl_woman_icon.png',
                  width: 12.w,
                  height: 12.w,
                ),
                SizedBox(width: 3.w),
                AhuText(
                  text: '${userInfo?.birth}',
                  fontSize: 12,
                  color: '#FE73CF'.stringToColor,
                  fontWeight: FontWeight.w400,
                ),
                SizedBox(width: 6.w),
              ],
            ),
          ),
          AhuText(
            text: '${userInfo?.nickName}',
            color: '#868686'.stringToColor,
            fontSize: 11,
            fontWeight: FontWeight.w400,
          )
        ],
      );
    }
    return Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        Container(
          padding: EdgeInsets.symmetric(vertical: 2.w),
          margin: EdgeInsets.only(right: 4.w),
          decoration: BoxDecoration(
            color: '#F3F7FF'.stringToColor,
            borderRadius: BorderRadius.circular(8.w),
          ),
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              SizedBox(width: 6.w),
              Image.asset(
                'images/user/txl_man_icon.png',
                width: 12.w,
                height: 12.w,
              ),
              SizedBox(width: 3.w),
              AhuText(
                text: '${userInfo?.birth}',
                fontSize: 12,
                color: '#739BFE'.stringToColor,
                fontWeight: FontWeight.w400,
              ),
              SizedBox(width: 6.w),
            ],
          ),
        ),
        AhuText(
          text: '${userInfo?.nickName}',
          color: '#868686'.stringToColor,
          fontSize: 11,
          fontWeight: FontWeight.w400,
        )
      ],
    );
  }

  Widget getCoverImgView({
    String actiCover = '',
    String title = '',
    String startTime = '',
    String endTime = '',
    String address = '',
    String actiCost = '',
    CrossAxisAlignment crossAxis = CrossAxisAlignment.start,
    MainAxisAlignment mainAxis = MainAxisAlignment.start,
    required FuturePopular dataModel,
  }) {
    return GestureDetector(
      onTap: () {
        Get.to(ActDetailsPage(
          dataModel: dataModel,
          actId: dataModel.activityId,
        ));
      },
      child: Column(
        crossAxisAlignment: crossAxis,
        children: [
          ClipRRect(
              borderRadius: BorderRadius.circular(5),
              child: getCoverImg(actiCover)),
          const SizedBox(
            height: 14,
          ),
          Text(
            title,
            style: TextStyle(fontSize: 14, color: ThemeColor.black2C2C2CColor),
          ),
          const SizedBox(
            height: 7,
          ),
          Row(
            children: [
              Image.asset(
                'images/scan/deails_money_type.png',
                width: 14,
              ),
              const SizedBox(
                width: 2,
              ),
              Text(
                actiCost,
                style:
                    TextStyle(color: ThemeColor.redE84D46Color, fontSize: 14),
              ),
            ],
          ),
          const SizedBox(
            height: 7,
          ),
          Row(
            children: [
              Image.asset(
                'images/scan/details_time_clock.png',
                width: 14,
              ),
              const SizedBox(
                width: 2,
              ),
              Text(
                startTime,
                style:
                    TextStyle(color: ThemeColor.gray99A3B4Color, fontSize: 14),
              )
            ],
          ),
          const SizedBox(
            height: 7,
          ),
          Row(
            children: [
              Image.asset(
                'images/scan/details_map_location.png',
                width: 14,
              ),
              const SizedBox(
                width: 2,
              ),
              Text(
                address,
                style:
                    TextStyle(color: ThemeColor.gray99A3B4Color, fontSize: 14),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Widget getCoverImg(String actiCover) {
    double imgWidth = 195;
    double imgHeight = 112;
    return actiCover.isNotEmpty
        ? ChatNetImage(
            imageUrl: actiCover,
            width: imgWidth,
            height: imgHeight,
          )
        : Image.asset(
            'images/map/oryj_logo_big.png',
            width: imgWidth,
            height: imgHeight,
          );
  }
}
